<template>
  <div class="Announcement">
    <div class="top flex-left">
      <!-- <div class="category-select flex-left" v-if="$sotre.state.userInfo.platform_id===1">
        <label>钱包</label>
        <el-select
          v-model="searchForm.type"
          placeholder="请选择"
          size="medium"
          @change="changeCategory"
        >
          <el-option v-for="(item,index) in list" :key="index" :label="item" :value="index+1"></el-option>
        </el-select>
      </div>-->
      <div class="type-select flex-left">
        <label>显示类别</label>
        <el-select
          v-model="searchForm.type"
          placeholder="请选择"
          size="medium"
          @change="searchForm.page=1;search();dialog.data.type=searchForm.type"
        >
          <el-option label="全部" :value="0"></el-option>
          <el-option label="滚动" :value="1"></el-option>
          <el-option label="弹窗" :value="2"></el-option>
        </el-select>
      </div>
      <el-button
        type="primary"
        size="medium"
        @click="dialog.show=true;dialog.type=0;dialog.data={type:dialog.data.type}"
      >+ &nbsp;发布系统消息</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="medium"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" prop="banner_url" label="创建时间">
          <template slot-scope="item">
            <span>{{item.row.create_time}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="title" label="标题"></el-table-column>
        <el-table-column align="center" label="内容">
          <template slot-scope="item">
            <el-button type="text" @click="edit(item.row,2)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="image_url" label="显示类型">
          <template slot-scope="item">{{item.row.type==='1'?'滚动':'弹窗'}}</template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="250">
          <template slot-scope="item">
            <el-button
              type="text"
              @click="show_hide(item,'1')"
              v-if="item.row.status==='2'"
              :loading="onChange===item.$index"
            >显示</el-button>
            <el-button
              type="text"
              @click="show_hide(item,'2')"
              v-if="item.row.status==='1'"
              :loading="onChange===item.$index"
            >隐藏</el-button>
            <el-button type="text" @click="edit(item.row,1)">编辑</el-button>
            <el-button type="text" style="color:red" @click="del(item.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <msg-box :visible="dialog" :id="searchForm.platform_id" @success="()=>search()"></msg-box>
      <!-- 分页 -->
      <pages :pages="searchForm" :total="total" @pageChange="pageChange" layout="prev, pager, next"></pages>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import MsgBox from "@/components/Message/Message.vue";
import { getMsgList, delMsg, updateMsg } from "./message.service";
export default {
  components: {
    Pages,
    MsgBox
  },
  data() {
    return {
      dialog: {
        show: false,
        type: 0, //0-添加,1-编辑,2-查看
        data: {
          type: "0"
        }
      },
      tableData: [],
      searchForm: {
        platform_id: this.$store.state.userInfo.platform_id,
        status: 0,
        type: 0,
        page: 1,
        size: 10
      },
      msgForm: {
        id: "",
        title: "",
        content: "",
        status: "",
        type: "",
        author: "",
        is_top: ""
      },
      total: 0,
      loading: false,
      onChange: ""
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.search();
    },
    async search() {
      this.loading = true;
      const res = await getMsgList(this.searchForm);
      this.tableData = res.data.list || [];
      this.total =
        Number(res.data.page.pageCount) * Number(res.data.page.pageSize);
      this.loading = false;
    },
    edit(item, type) {
      this.dialog = {
        show: true,
        type: type,
        data: item
      };
    },
    changeType() {},
    async del(item) {
      // this.$confirm(
      //   `确定删除消息 <span class="confirm-tip-color">${item.title}</span> ？`,
      //   "提示",
      //   {
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     type: "warning",
      //     dangerouslyUseHTMLString: true
      //   }
      // )
      //   .then(() => {
      delMsg(item.id).then(res => {
        if (res.code === 0) {
          this.$notify.success({
            title: "提示",
            message: "删除成功",
            duration: 1500
          });
          this.search();
        } else {
          this.$notify.error({
            title: "提示",
            message: "删除失败，请稍后重试。",
            duration: 1500
          });
        }
      });
      // })
      // .catch(() => {
      //   this.$message.info("已取消删除。");
      // });
    },
    async show_hide(item, type) {
      item.row.status = type;
      this.onChange = item.$index;
      const res = await updateMsg(item.row);
      if (res.code === 0)
        this.$notify.success({
          title: "提示",
          message: "修改成功"
        });
      this.onChange = "";
      this.search();
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search(this.searchForm);
    },
    pageSizeChange(val) {
      this.searchForm.size = val;
      this.search(this.searchForm);
    }
  }
};
</script>

<style lang="scss">
.Announcement {
  position: relative;
  min-height: calc(100vh - 100px);
  .top {
    .category-select {
      width: 230px;
      margin-right: 30px;
      color: #606266;
      label {
        width: 60px;
        flex-shrink: 1;
      }
    }
    .input-with-select {
      width: 341px;
      .el-input-group__append {
        background-color: #66b1ff;
        color: #fff;
      }
    }
    .type-select {
      margin-right: 20px;
      label {
        width: 5em;
      }
      .el-select {
        width: 120px;
      }
    }
  }
  .data-table {
    position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 20px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .scaleImg {
      max-width: 120px;
      max-height: 23px;
      cursor: pointer;
    }
    .link {
      color: #409eff !important;
    }
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
}
</style>